/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.adapters;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itgrail.grail.ddd.annotation.infrastructure.Adapter;
import com.itgrail.pilot.domain.user.entity.DeptE;
import com.itgrail.pilot.domain.user.ports.DeptPort;
import com.itgrail.pilot.infrastructure.converter.DeptConverter;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import org.springframework.beans.factory.annotation.Autowired;


@Adapter
public class DeptAdapter implements DeptPort {

    @Autowired
    private DepartmentMapper mapper;

    @Autowired
    private DeptConverter converter;


    @Override
    public Integer addDept(DeptE deptE) {
        DepartmentDO deptDO = converter.entityToData(deptE);
        return mapper.insert(deptDO);
    }

    @Override
    public Integer updateDept(DeptE deptE) {
        DepartmentDO deptDO = converter.entityToData(deptE);
        return mapper.updateById(deptDO);
    }

    @Override
    public DeptE findOne(Integer deptId) {
        LambdaQueryWrapper<DepartmentDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartmentDO::getId, deptId);
        DepartmentDO deptDO = mapper.selectOne(queryWrapper);
        return converter.dataToEntity(deptDO);
    }

    @Override
    public DeptE findById(Integer id) {
        return converter.dataToEntity(mapper.selectById(id));
    }

    @Override
    public void delete(DeptE deptE) {
        mapper.deleteById(deptE.getId());
    }

}
